package com.xxxx.yeb.service.impl;

import com.xxxx.yeb.mapper.EmployeeMapper;
import com.xxxx.yeb.pojo.Department;
import com.xxxx.yeb.mapper.DepartmentMapper;
import com.xxxx.yeb.pojo.RespBean;
import com.xxxx.yeb.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-07-17
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 查询所有部门
     *
     * @return 返回部门list
     */
    public List<Department> selectAllDepartment() {
      return   departmentMapper.selectList(null);
    }

    /**
     * 查询部门
     * @return
     */
    public List<Department> getDePartmentByid() {

        return departmentMapper.getDePartmentByid(-1) ;
    }

    /**
     * 添加部门
     * @param dep
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public RespBean addDepartment(Department dep) {
        //获取添加用户
        Integer succeed = departmentMapper.insert(dep);
        if (succeed>0){
            //查询父部门的信息
            Department  result = departmentMapper.queryDep(dep.getParentId());
            //设置父部门的isParent
            if (result.getIsParent()==0){
                departmentMapper.updateDep(1,dep.getParentId());
            }
            /**
             * 为新部门添加depPath
             */
            //查询新部门id
            String subId = departmentMapper.querySubDep(dep.getName());
            //查询父部门路径
            String father = result.getDepPath();
            //新部门路径
            String depPath = father.concat("."+subId);
            //更新新部门路径
            departmentMapper.updateSubDep1(depPath,dep.getName());

            return RespBean.success("部门添加成功");

        }
        return RespBean.error("部门添加失败，请重试");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public RespBean deleteDepartment(Integer id) {
        //查询删除id的子部门信息
        Department dep = departmentMapper.queryDepartment(id);
        if (dep == null){
            //通过id把部门设置为null
            employeeMapper.queryDepartement(id);
            //查询子部门的信息
            Department subDep = departmentMapper.querySubDepartment(id);
            //如果没有子部门更新要删除部门的enabled
            Integer re = departmentMapper.deleteDepartment(id);
            //查询要删除部门的父部门还有没有子部门
            Department fatherDep = departmentMapper.queryFatherDepartment(subDep.getParentId());
            if (fatherDep == null){
                //查询父部门的isParent是否为1
                Department  fatherResult = departmentMapper.queryDep(subDep.getParentId());
                if (fatherResult.getIsParent()!=0){
                    departmentMapper.updateDepartment(subDep.getParentId());

                }
            }
            return RespBean.success("部门删除成功");
        }
        return  RespBean.error("删除部门失败");
    }



}
